package day19;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


/**
 * 使用字节缓冲流复制一个mp3文件
 * @author wangbiao
 *
 */
public class CopyMp3ByBuffer {

	public static void main(String[] args) {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			//bis = new BufferedInputStream(new FileInputStream("oo.mp3"));
			bis = new BufferedInputStream(new FileInputStream("oo.mp3"));
			bos = new BufferedOutputStream(new FileOutputStream("yy.mp3"));
			byte[] buf = new byte[1024*2];
			int len = 0;
			while ((len=bis.read(buf)) != -1) {
				bos.write(buf, 0, len);
				bos.flush();
			}
		} catch (FileNotFoundException e) {
			throw new RuntimeException("复制mp3失败！");
		} catch (IOException e) {
			throw new RuntimeException("复制mp3失败！");
		} finally {
			if (null !=bis) {
				try {
					bis.close();
				} catch (IOException e) {
					throw new RuntimeException("关闭字节缓冲读取流失败！");
				}
			}
			if (null !=bos) {
				try {
					bos.close();
				} catch (IOException e) {
					throw new RuntimeException("关闭字节缓冲写入流失败！");
				}
			}
		}
	}
}
